package com.armani.controller;

import com.alibaba.fastjson.JSON;
import com.armani.entity.ItemType;
import com.armani.model.Result;
import com.armani.model.TypeModel;
import com.armani.service.TypeService;
import com.armani.util.JsonUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName TypeController
 * @Description 商品类别控制层
 * @Author Admin
 * @Date 2021/11/27 11:04
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/type/", produces = "text/html;charset=UTF-8")
public class TypeController {

    @Autowired
    //自动装配
    private TypeService typeService;

    @GetMapping(value = "listSearch.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String pageSearch(@RequestParam Integer page,
                             @RequestParam Integer limit,
                             @RequestParam(required = false) Integer id) {
        Integer startRow = (page - 1) * limit;
        List<ItemType> types = typeService.pageSearch(startRow, limit, id);
        Integer count = typeService.getCount(id);
        Result result = new Result();
        result.setCount(count);
        result.setData(types);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "insert.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String insert(@RequestParam String title,
                         @RequestParam Integer parentId,
                         @RequestParam Integer sort) {
        Integer insert = typeService.insert(title, parentId, sort);
        Result result = new Result();
        result.setData(insert);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "update.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String update(@RequestParam Integer id,
                         @RequestParam String title,
                         @RequestParam Integer parentId,
                         @RequestParam Integer sort) {
        Integer update = typeService.update(id, title, parentId, sort);
        Result result = new Result();
        result.setData(update);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    @PostMapping(value = "delete.do", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String delete(@RequestParam Integer id) {
        Integer update = typeService.delete(id);
        Result result = new Result();
        result.setData(update);
        result.setCode(0);
        String json = JSON.toJSONString(result);
        return json;
    }

    /**
     * 查询所有分类并设置子节点
     *
     * @return 分类列表集合(json)
     */
    @RequestMapping(value = "list.do")
    @ResponseBody
    public String typeList() {
        List<TypeModel> typeList = typeService.typeList();
        Result result = new Result();
        result.setCode(0);
        result.setData(typeList);
        result.setMsg("查询成功");
        return JsonUtil.stringify(result);
    }

    @RequestMapping(value = "add.do")
    @ResponseBody
    public String typeAdd(@RequestParam String title,@RequestParam Integer parentId) {
        Map<String ,Object> params=new HashMap<>();
        params.put("title",title);
        params.put("parentId",parentId);
        Integer row = typeService.typeAdd(params);
        Result result = new Result();
        if (row != null) {
            result.setCode(0);
            result.setMsg("添加成功！");
        } else {
            result.setCode(1000);
            result.setMsg("添加失败");
        }
        return JsonUtil.stringify(result);

    }
}
